package ru.spbau.design.snmp.android.viewer.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ru.spbau.design.snmp.android.viewer.R;
import ru.spbau.design.snmp.android.viewer.model.*;
import ru.spbau.design.snmp.android.viewer.provider.NetworkSnapshotProviderFile;
import ru.spbau.design.snmp.android.viewer.provider.NetworkSnapshotProviderRemote;
import ru.spbau.design.snmp.android.viewer.searcher.SearchResult;
import ru.spbau.design.snmp.android.viewer.searcher.SearcherSimple;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.ExpandableListAdapter;

public class ViewerActivity extends Activity {
	// Self

	ViewerActivity viewerActivity;
	Accessor accessor;

	// UI aggregators

	ConfigurationTab configTab;
	RouterTab routerTab;
	SubnetsTab subnetsTab;
	SearchTab searchTab;

	// Views

	TabHost tabs;

	// Viewer state

	NetworkSnapshot networkSnapshot;
	List<Network> networks;
	Network network;

	boolean initialized;
	
	List<Listener> listeners;
	
	// --------------------------------------------------------------------------------
	
	public static class Accessor {
		ViewerActivity viewerActivity;
		
		public Accessor(ViewerActivity va) {
			this.viewerActivity = va;
		}
		
		public Network network() {
			return this.viewerActivity.network;
		}
		
		public ViewerActivity context() {
			return this.viewerActivity;
		}
		
		public Rev getRouterPos(NetInterface iface) {
			return this.viewerActivity.routerTab.getRouterPos(iface);
		}
		
		public int getRouterPos(Router router) {
			return getRouterPos(router.getInterfaces().get(0)).getGroup();
		}
		
		public Rev getSubnetPos(NetNode subnet) {
			return this.viewerActivity.subnetsTab.getSubnetPos(subnet);
		}
		
		public int getSubnetPos(SubNet subnet) {
			return getSubnetPos(subnet.getNodes().get(0)).getGroup();
		}
		
		public TabHost tabs() {
			return this.viewerActivity.tabs;
		}
		
		public ExpandableListView routersView() {
			return this.viewerActivity.routerTab.getView();
		}
		
		public ExpandableListView subnetsView() {
			return this.viewerActivity.subnetsTab.getView();
		}
		
		public List<Network> networks() {
			if (this.viewerActivity.networkSnapshot == null) {
				return null;
			}
				
			return this.viewerActivity.networkSnapshot.getNetworks();
			
		}
		
		public NetworkSnapshot snapshot() {
			return this.viewerActivity.networkSnapshot;
		}
	}
	
	// --------------------------------------------------------------------------------

	@Override
	public void finish() {
		super.finish();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.listeners = new ArrayList<Listener>();		
		this.accessor = new Accessor(this);	

		this.networks = new ArrayList<Network>();

		this.viewerActivity = this;

		this.setContentView(R.layout.main);

		initialized = false;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (initialized == true) {
			return;
		}

		this.tabs = (TabHost) this.findViewById(android.R.id.tabhost);
		
		this.configTab = new ConfigurationTab(this.accessor);
		this.routerTab = new RouterTab(this.accessor);
		this.subnetsTab = new SubnetsTab(this.accessor);
		this.searchTab = new SearchTab(this.accessor);

		this.tabs.setup();

		// Generate tabbed interface

		this.tabs.addTab(tabs.newTabSpec("Config").setContent(
				R.id.configurationView).setIndicator("Config"));
		this.tabs.addTab(tabs.newTabSpec("Routers")
				.setContent(R.id.routersView).setIndicator("Routers"));
		this.tabs.addTab(tabs.newTabSpec("Subnets")
				.setContent(R.id.subnetsView).setIndicator("Subnets"));
		this.tabs.addTab(tabs.newTabSpec("Search").setContent(R.id.searchView)
				.setIndicator("Search"));

		// Setup user interaction listeners

		this.initialized = true;
	}

	// --------------------------------------------------------------------------------
	
	public void subscribe(Listener listener) {
		this.listeners.add(listener);
	}
	
	private void configurationChanged() {
		for (Listener listener : this.listeners) {
			listener.onConfigurationChanged();
		}
	}
	
	// --------------------------------------------------------------------------------
	
	public void updateConfiguration(int netId) {
		this.network = this.networks.get(netId);		
		configurationChanged();
	}

	public void updateConfiguration(String collectorAddress, int collectorPort) {
		try {
			this.networkSnapshot = (new NetworkSnapshotProviderRemote(
					collectorAddress, collectorPort))
					.getNetworkSnapshot();
			this.network = this.networkSnapshot.getNetworks().get(0);
		
			System.out.println("Updated configuration, " + String.valueOf(this.network.getRouters().size()) + " routers");
			
			configurationChanged();
			this.tabs.setCurrentTabByTag("Routers");
		} catch (Exception e) {
			System.out.println("An exception is caught: " + e.getMessage());
			e.printStackTrace();

			AlertDialog ad = (new android.app.AlertDialog.Builder(this))
					.create();
			ad.setMessage(e.getMessage());
			ad.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			ad.show();
		}
	}
}